//
//  HomeCollectViewController.m
//  CarUser
//
//  Created by 王森林 on 2018/12/5.
//  Copyright © 2018年 王森林. All rights reserved.
//

#import "HomeCollectViewController.h"
#import "HomeCollectCell.h"
#import "UserRes.h"
#import "Car_CarDetailViewController.h"
@interface HomeCollectViewController()
@property (nonatomic, strong) NSMutableArray *listAry;
@end

@implementation HomeCollectViewController

-  (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"收藏夹";
//    [self setNaviDefaultRightBtnWithTitle:@"编辑" andSel:@selector(handleCollectAction)];
    [self  createUI];
    [self fetchDataFromNet];
    self.mainTableView.mj_header = [Custom_Mjfresh mj_customMjfreshHeaderSetting:^{
        [self fetchDataFromNet];
    }];
}


- (void)fetchDataFromNet
{
    //收藏夹列表
    UserRes *user = [CommonMethod getUser];
    if (!user) {
        [[NSNotificationCenter defaultCenter]postNotificationName:NotificationCenterName_AlertLoginVC object:nil];
        return;
    }
    NSDictionary *params = @{
                             @"FKEY":[CommonMethod createMD5Key:@"TOKEN"],
                             @"TOKEN":user.TOKEN,
                             @"USERNAME":user.mobile
                             
                             };
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [SVProgressHUD showWithStatus:nil];
    });
    
    [[ZY_AfnNetManager shareInstance]postWithURL:Car_API_43 params:params md5Paraname:nil success:^(id  _Nullable response) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
        });
        [self.mainTableView.mj_header endRefreshing];
        [self.listAry removeAllObjects];
        NSDictionary *resultDict  = response;
//        self.listAry = [NSMutableArray arrayWithArray:[resultDict objectForKey:@"list"]];
//        [self.mainTableView reloadData];
        
        NSArray *ary = [resultDict objectForKey:@"list"];
        if (ary != nil && ![ary isKindOfClass:[NSNull class]] && ary.count != 0)
        {
            [self hideNoDataView];
            self.listAry = [NSMutableArray arrayWithArray:[resultDict objectForKey:@"list"]];
            
        } else {
            
            [self showNodataView:self.mainTableView.centerY-150 withView:self.mainTableView];
            
        }
        [self.mainTableView reloadData];
        

        
        
    } fail:^(NSError * _Nonnull error) {
        dispatch_async(dispatch_get_main_queue(), ^{
              [self.mainTableView.mj_header endRefreshing];
            [SVProgressHUD showErrorWithStatus:error.localizedDescription];
        });
        
    }];
    
    
    
}



- (void)createUI
{
    
     self.mainTableView.frame  = CGRectMake(0, 0, BOUNDWIDTH, BOUNDHEIGHT-(IS_IPHONE_X?88:64));
    [self.mainTableView registerClass:[HomeCollectCell class] forCellReuseIdentifier:@"HomeCollectCell"];
   

}


#pragma mark - 代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.listAry.count;
}
#pragma mark - 代理方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 120.0f;
}


//}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HomeCollectCell *cell = (HomeCollectCell *)[tableView dequeueReusableCellWithIdentifier:@"HomeCollectCell" forIndexPath:indexPath];
    [cell setData:self.listAry[indexPath.row]];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
     NSDictionary *dict = [self.listAry objectAtIndex:indexPath.row];
   
        Car_CarDetailViewController *push = [[Car_CarDetailViewController alloc]init];

        push.SERIES_ID = [dict objectForKey:@"ZSCXID"];
        [self.navigationController pushViewController:push animated:YES];
    
}

- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dict = [self.listAry objectAtIndex:indexPath.row];
    
//    __weak typeof(self)weakSelf = self;

    
    UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        //添加收藏
        UserRes *user = [CommonMethod getUser];
        if (!user) {
            [[NSNotificationCenter defaultCenter]postNotificationName:NotificationCenterName_AlertLoginVC object:nil];
            return;
        }
        NSDictionary *params = @{
                                 @"FKEY":[CommonMethod createMD5Key:@"FAVORITE_ID"],
                                 @"TOKEN":user.TOKEN,
                                 @"USERNAME":user.mobile,
                                 @"FAVORITE_ID":[dict objectForKey:@"FAVORITE_ID"]
                                 
                                 };
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD showWithStatus:nil];
        });
        
        [[ZY_AfnNetManager shareInstance]postWithURL:Car_API_44 params:params md5Paraname:nil success:^(id  _Nullable response) {
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [SVProgressHUD dismiss];
            });
            
            [self  fetchDataFromNet];      
            
        } fail:^(NSError * _Nonnull error) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [SVProgressHUD showErrorWithStatus:error.localizedDescription];
            });
            
        }];
    }];
    return @[action1];
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}


@end
